/**********************************************
 * 	Word.java								  *
 *  Course: CS3343							  *
 *  Version: 2.0    Release 1				  *
 *  Last Modified : 22 MAR 2013			      *
 **********************************************/
package checker;

import file.FileIO;
import java.util.ArrayList;

public abstract class Word {
	protected FileIO fileIO;
	private SentenceChecker sentenceChecker;
	private String sign;
	private String type;
	private String modifiedArticle;

	public Word(String sign, String type) {
		this.sign = sign;
		fileIO = new FileIO();
		sentenceChecker = new SentenceChecker();
		modifiedArticle = "";
		this.type = type;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getType() {
		return type;
	}

	public String getModifiedArticle() {
		return modifiedArticle;
	}

	public abstract boolean isOfType(String word) ;

	// this function finds a specific type of words in an article
	public String[] find(String article, Word type) {

		String[] words;
		String foundWords = "";
		String foundIndicesAndWords =  "\r\n"+ this.type + ":\r\n"; // output indices and words in each type
		String[] wordsInArticle = fileIO.getFileContents(article).trim().split(" ");
		String word = "";
		String punctuation = "";
		for (int i = 0; i < wordsInArticle.length; i++) {
			// get rid of the punctuation for comparison
			punctuation = sentenceChecker.endWithPunctuation(wordsInArticle[i]) ? wordsInArticle[i].charAt(wordsInArticle[i].length() - 1) + "": "";
			word = sentenceChecker.endWithPunctuation(wordsInArticle[i]) ? wordsInArticle[i].substring(0, wordsInArticle[i].length() - 1): wordsInArticle[i];
			if (type.isOfType(word)) {
				// concatenate all the identified words into a string
				foundWords += i + "/" + word + " ";
				foundIndicesAndWords += "[" + i + "]" + word + "\r\n";
				// bracket the identified words
				word = word + sign + punctuation + " ";
			} else {
				// no bracketing for other types of words in printing
				word = word + punctuation + " ";
			}

			// store the whole modified article into a string
			modifiedArticle += word;

			if (i != 0 && i % 10 == 0) {
				modifiedArticle += "\r\n";

			}
		}

		// split the concatenated string using space separator and store them
		// into an array
		// example format: 2/year. it means the word "year" in the third index
		// location is identified
		words = foundWords.split(" ");

		fileIO.writeToFile(modifiedArticle + foundIndicesAndWords, "Found" + this.type + ".txt");
		return words;
	}

	public ArrayList<String[]> findAll(String article, String outputName) {
        
		
		//index 0: array containing indices of Adjectives in the string
		//index 0: array containing indices of Nouns in the string
		//index 0: array containing indices of Verbs in the string
		//index 0: array containing indices of Adverbs in the string
		//index 0: array containing indices of Prepositions in the string
		ArrayList<String[]> allTypes = new ArrayList<String[]>();
		
		//used to check if a word is in one of the following categories.
		Word[] words = { new Adjective("[adj]"), new Noun("[n]"),
				new Verb("[v]"), new Adverb("[adv]"), new Preposition("[pre]") };

		String foundWords = "";
		String foundIndicesAndWords = ""; // output indices and words in each type
		String[] wordsInArticle = fileIO.getFileContents(article).trim().split(" ");
		String word = "";
		String punctuation = "";
		foundIndicesAndWords += "\r\n";
		for (int j = 0; j < words.length; j++) {
			foundIndicesAndWords += "\r\n";
			foundIndicesAndWords = foundIndicesAndWords + words[j].getType() + ":\r\n";
			
			System.out.println("Processing " + words[j].getType() + ".........");
			for (int i = 0; i < wordsInArticle.length; i++) {
				// get rid of the punctuation for comparison
				punctuation = sentenceChecker.endWithPunctuation(wordsInArticle[i]) ? wordsInArticle[i].charAt(wordsInArticle[i].length() - 1) + "": "";
				word = sentenceChecker.endWithPunctuation(wordsInArticle[i]) ? wordsInArticle[i].substring(0, wordsInArticle[i].length() - 1): wordsInArticle[i];
				if (words[j].isOfType(word)) {
					// concatenate all the identified words into a string
					foundWords += i + "/" + word + " ";
					foundIndicesAndWords += "[" + i + "]" + word + "\r\n";
					// bracket the identified words
					word = word + words[j].getSign() + punctuation + " ";
				} else {
					// no bracketing for other types of words in printing
					word = word + punctuation + " ";
				}
       
				// store the whole modified article into a string
				modifiedArticle += word;

				// only applicable to the final round of processing
				// contents in this variable will be output to a text file
				if (j == words.length - 1 && i != 0 && i % 10 == 0) {
					modifiedArticle += "\r\n";
				}
				
			}
			
			// split the concatenated string using space separator and store them into an array
			// example format: 2/year. it means the word "year" in the third index location is identified
			allTypes.add(foundWords.split(" "));
			
			// use the result of the previous round of processing as the input
			// to the next round
			wordsInArticle = modifiedArticle.split(" ");
			if (j != words.length - 1) {
				modifiedArticle = "";
				foundWords = "";
			}
			

		}// for loop
       
		System.out.println("All words have been processed! \nWriting output to file named " + outputName + ".......");
		fileIO.writeToFile(modifiedArticle+ foundIndicesAndWords, outputName);

		return allTypes;

	}
}
